package
{
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SampleDataEvent;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.media.Microphone;
	import flash.media.Sound;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetStream;
	import flash.net.NetConnection;
	import flash.text.TextField;
	import flash.text.TextFieldType
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	

	
	public class RTMPprobak extends Sprite
	{
		//var SERVER:String = "rtmp://192.168.1.132/oflaDemo";
		//var VIDEO_PATH:String = "/toystory3.flv";
		var streamClient:Object = new Object();
		var fullscreen:Boolean = false;
		var mute:Boolean = false;
		var recordingNow:Boolean = false;
		
		public function RTMPprobak()
		
		{
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			drawBackground();
			
			var microphone:Microphone = Microphone.getMicrophone();
			var bytes:ByteArray = new ByteArray();
			var transform:SoundTransform = new SoundTransform();
			var connection:NetConnection = new NetConnection();
						
			var connectServer:TextField = createTextField(10, 10, stage.stageHeight / 23, stage.stageWidth * 0.65, 20, "rtmp://192.168.1.132");
			var videoPathServer:TextField = createTextField(10, 20 + (stage.stageHeight / 23), stage.stageHeight / 23, stage.stageWidth * 0.65,	20, "toystory3");
			
			var exitButton:Sprite = new createSprite(10, ((stage.stageHeight)/6)*5, stage.stageWidth - 20, (stage.stageHeight/6) - 10, 0x000000, "Exit application");
			exitButton.addEventListener(MouseEvent.CLICK, onExit);
			
			var resumeButton:Sprite = new createSprite(10, (stage.stageHeight*0.65) + 5, (stage.stageWidth / 4) - 10, stage.stageHeight/8, 0x000000, "Resume");
			resumeButton.addEventListener(MouseEvent.CLICK, onResume);
			
			var pauseButton:Sprite = new createSprite((stage.stageWidth / 4) + 5, (stage.stageHeight*0.65) + 5, (stage.stageWidth / 4) - 10, stage.stageHeight/8, 0x000000, "Pause");
			pauseButton.addEventListener(MouseEvent.CLICK, onPause);
			
			var muteButton:Sprite = new createSprite((stage.stageWidth / 2) + 5, (stage.stageHeight*0.65) + 5, (stage.stageWidth / 4) - 10,	stage.stageHeight/8, 0x000000, "Mute");
			muteButton.addEventListener(MouseEvent.CLICK, onMute);
			
			var recordButton:Sprite = new createSprite((stage.stageWidth / 4)*3, (stage.stageHeight*0.65) + 5, (stage.stageWidth / 4) - 10,	stage.stageHeight/8, 0x000000, "Record");
			recordButton.addEventListener(MouseEvent.CLICK, onRecord);
			
			var connectButton:Sprite = new createSprite(stage.stageWidth * 0.69, 10, stage.stageWidth * 0.30, stage.stageHeight / 10, 0x000000,	"Connect");
			connectButton.addEventListener(MouseEvent.CLICK, onConnect);			
			
			var video:Video = new Video();
			video.width = stage.stageWidth;
			video.height = stage.stageHeight / 2;
			video.y = stage.stageHeight*0.125;
			
			
						
			
			function onConnect():void{
				if (!connection.connected){
					connection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionEvent);
					connection.connect(connectServer.text + "/oflaDemo");
					
					
				} else {
					streamClient.close();
					connection.close();
					removeChild(video);
					stage.removeEventListener(Event.RESIZE, onResize);
				}
			}
					

			function netConnectionEvent(event:NetStatusEvent):void {
				event.target.removeEventListener(NetStatusEvent.NET_STATUS,
					netConnectionEvent);
				if (event.info.code == "NetConnection.Connect.Success") {
					var stream:NetStream = new NetStream(connection);
					var outStream:NetStream = new NetStream(connection);
					outStream.attachAudio(microphone);
					stream.addEventListener(NetStatusEvent.NET_STATUS, onStreamEvent);
					streamClient.onMetaData = onMetaData;
					streamClient = stream;
					video.attachNetStream(stream);
					stream.play(videoPathServer.text + ".flv");
					addChild(video);
					stage.addEventListener(Event.RESIZE, onResize);
					stream.publish("proba", "record");
					}
				}
			
			function drawBackground():void{
				var background:Sprite = new Sprite();
				background.graphics.beginFill(0x333333, 0.5);
				background.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
				background.graphics.endFill();
				addChild(background);
			}
			function createSprite(xPos: int, yPos: int, x:int, y:int, col: int, intext:String):Sprite{
				var temp:Sprite = new Sprite();
				temp.graphics.beginFill(col, 0.5);
				temp.graphics.drawRect(xPos, yPos, x, y);
				temp.graphics.endFill();
				var temptext:TextField = new TextField;
				var mytextformat:TextFormat = new TextFormat;
				mytextformat.size = 20;
				mytextformat.align = "center";
				mytextformat.color = 0xFFFFFF;
				temptext.text = intext;
				temptext.width = x;
				temptext.x = xPos;
				temptext.y = yPos + (y*0.35);
				temptext.setTextFormat(mytextformat);
				addChild(temp);
				temp.addChild(temptext);
				return temp;
			}
			
			function createTextField(x:int, y:int, height:int, width:int, fontSize: int, text:String):TextField{
				var temp:TextField = new TextField;
				temp.type = TextFieldType.INPUT;
				temp.border = true;
				var mytextFormat:TextFormat = new TextFormat;
				mytextFormat.size = fontSize;
				temp.defaultTextFormat = mytextFormat;
				temp.text = text;
				temp.x = x;
				temp.y = y;
				temp.height = height;
				temp.width = width;
				temp.multiline = true;
				temp.wordWrap = true;
				addChild(temp);
				return temp;
			}
			
			function onExit(event:MouseEvent):void{
				NativeApplication.nativeApplication.exit();
			}
			
			function onResume():void{
				streamClient.resume();
			}
			
			function onPause():void{
				streamClient.pause();
			}
			
			function onMute():void{
				if (!mute){
				streamClient.soundTransform = new SoundTransform(0);
				mute = true;
				} else {
					streamClient.soundTransform = new SoundTransform(20);
					mute = false;
				}
			}
			
			function onRecord():void {
				if (microphone!= null) {
					microphone.gain = 100;
					microphone.rate = 22;
					microphone.setSilenceLevel(0, 4000);
					microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, recording);
				}
			}
			
			function recording(event:SampleDataEvent):void {
				if (!recordingNow) {
				//	recordButton.removeEventListener(MouseEvent.CLICK, recording);
				//	recordButton.addEventListener(MouseEvent.CLICK, stopRecording);
					recordingNow = true;
					var timer:Timer = new Timer(5000);
					timer.addEventListener(TimerEvent.TIMER, stopRecording);
					timer.start();
				while(event.data.bytesAvailable) {
					var sample:Number = event.data.readFloat();
					bytes.writeFloat(sample);
					}
				}
			}
			
			function stopRecording():void {
				microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, recording);
			//	recordButton.removeEventListener(MouseEvent.CLICK, stopRecording);
			//	recordButton.addEventListener(MouseEvent.CLICK, recording);
				recordingNow = false;
				var sound:Sound = new Sound();
				sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playback);
				sound.play();
			}	
			
			
			function playback(event:SampleDataEvent):void {
				var sample:Number;
				for (var i:int = 0; i < 8192; i++) {
					if (bytes.bytesAvailable > 0) {
						sample = bytes.readFloat();
						event.data.writeFloat(sample); // channel left
						event.data.writeFloat(sample); // channel right
					}
				}
			}
			
			function onResize():void{
				if (!fullscreen){
				video.y = 0;
				video.height= stage.stageHeight;
				video.width = stage.stageWidth;
				fullscreen = true;
				removeChild(connectServer);
				removeChild(videoPathServer);
				removeChild(exitButton);
				removeChild(resumeButton);
				removeChild(pauseButton);
				removeChild(muteButton);
				removeChild(recordButton);
				removeChild(connectButton);
				} else { 
					video.width = stage.stageWidth;
					video.height = stage.stageHeight / 2;
					video.y = stage.stageHeight*0.125;
					fullscreen = false;
					addChild(connectServer);
					addChild(videoPathServer);
					addChild(exitButton);
					addChild(resumeButton);
					addChild(pauseButton);
					addChild(muteButton);
					addChild(recordButton);
					addChild(connectButton);
				}					
			}
			function onStreamEvent():void{				
				
			}
			
			function onMetaData(data:Object):void{
				trace("width: " + data.width);
				trace("height: " + data.height);

			}
		}
	}
}